﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;
using System.Data;

namespace VINETLIFE.Module
{
    public partial class ucProductCategory : System.Web.UI.UserControl
    {
        cmsProductBL data = new cmsProductBL();
        cmsProductDO itemProductDO = new cmsProductDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["shop"] == null)
                {
                    Session["shop"] = new MyShopCart();
                }
                rptHomePageCateDataSource();
                rptEcogoDataSource();
                rptTopbikeDataSource();
            }
        }
        protected void rptEcogoDataSource()
        {
            rptEcogo.DataSource = new cmsProductBL().GetByOriginID(2).Rows.Cast<System.Data.DataRow>().Take(4).CopyToDataTable();
            rptEcogo.DataBind();
        }
        protected void rptTopbikeDataSource()
        {
            rptTopbike.DataSource = new cmsProductBL().GetByOriginID(3).Rows.Cast<System.Data.DataRow>().Take(4).CopyToDataTable();
            rptTopbike.DataBind();
        }
        private void rptHomePageCateDataSource()
        {
            DataTable dt = new cmsProductCategoryBL().SelectHomePage();
            /*      string s = "<script type='text/javascript'>$(document).ready(function () {";
                  for (int i = 0; i < dt.Rows.Count; i++)
                  {
                      object x = dt.Rows[i]["ProductCategoryID"];
                      s += "$('#foo-" + x + "').carouFredSel({circular: true,items: { visible: 4 },width: 757,scroll: { duration: 2000, pauseOnHover: true },next: '#slide-next-" + x + "',prev: '#slide-prev-" + x + "'});";
                  }
                  s += "});</script>";
                  lblRunScript.Text = s;*/
            rptHomePageCate.DataSource = dt;
            rptHomePageCate.DataBind();
        }

        protected void rptHomePageCate_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;

                Repeater rptChild = (Repeater)e.Item.FindControl("rptChild");
                int productCategoryID = int.Parse(drv["ProductCategoryID"].ToString());

                //rptChild.DataSource = new cmsProductBL().SelectByProductCategory(productCategoryID);
                rptChild.DataSource = new cmsProductBL().SelectByProductCategoryHomepage(productCategoryID);
                rptChild.DataBind();
            }
        }
        protected string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
        protected void rptChild_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int productID = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName.Equals("AddToCart"))
            {
                AddItem(productID);
            }
        }
        private void AddItem(int productID)
        {
            cmsProductDO itemDO = new cmsProductDO();
            itemDO.ProductID = productID;
            //itemDO.ProductID = idProduct;
            cmsProductDO product = data.Select(itemDO);
            if (product != null)
            {
                ProductShop item = new ProductShop();
                item.Id = product.ProductID;
                item.Image = product.ProductImage;
                item.Name = product.ProductTitle;
                if (product.IsPromotion) //  Kiểm tra sản phẩm có khuyến mãi thì tính giá khuyến mãi(saleprice). ko thì tính giá bình thường(price)
                {
                    if (product.SalePrice == 0 || product.SalePrice.ToString().Equals(""))
                    {
                        item.Cost = 0;
                    }
                    else
                    {
                        item.Cost = (decimal)product.SalePrice;
                    }
                }
                // Sản phẩm không có giá khuyến mãi
                else
                {
                    if (product.Price == 0 || product.Price.ToString().Equals(""))
                        item.Cost = 0;
                    else
                    {

                        item.Cost = (decimal)product.Price;
                    }
                }
                item.Quantity = 1;
                ((MyShopCart)Session["shop"]).Add(item);
            }
            //Response.Redirect(this.Request.Url.ToString());
            Ultility.Alert("Đặt hàng thành công", Request.Url.AbsolutePath);
            // ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>$(document).ready(fuction(){ $('[id*=btnPopup]').live('click', function () { $('#dialog').dialog({title: 'jQuery Dialog Popup',buttons: { Close: function () {$(this).dialog('close'); }} }); return false;}); });</script>", false);
        }
    }
}